<?php
/**
 * Created by PhpStorm.FUN-AC-0038-BD情人节活动
 * User: allen.qiang
 * Date: 2015-01-27
 * Time: 下午13:50
 */
quanli_require('module/base/activity/activityConfig.class.php');

class activityValentine {

    public $configName="Valentine_BD";//activities_extend表活动编号
    /**
     * 得到活动的信息
     * @return $activity_array
     * @author  2015-01-27 allen.qiang <allen.qiang@fistforward.com>
     */
    public function activity_master($pcustID){
        global $G_SHOP;


        //实例化配置类
        $activity_config = new activityConfig($this->configName); //活动配置
        $config_array = $activity_config->config;

        //sql字符串
        $sql = " AND activityNumber='".$this->configName."' ";

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        $secret_key="";//秘钥
        $love_letter="";//情书内容

        //会员ID
        if (isset($pcustID) && !empty($pcustID)) {
            $sql .= ' AND extend_1 = ?';
            $params_type .= 's';
            $params_array[] = $pcustID;

            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<判断用户是否已经填写了情书>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            $sql_master="SELECT * FROM `activities_extend` WHERE classifyID='0' ".$sql;
            $temp_item_master = &$G_SHOP->DBCA->stmt_query($sql_master,$params_type,$params_array);
            if($temp_item_master){
                $secret_key = $temp_item_master[0]->extend_4;
                $love_letter = $temp_item_master[0]->extend_9;
            }

        }


        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<得到活动蛋糕数量>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $sql_master2="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND billPromotion=".$config_array['billPromotion'];
        $item_master = &$G_SHOP->DBCA->stmt_query($sql_master2);
        $number=$config_array['cake_sum'];
        if($item_master){
            $number =$number- $item_master[0]->item_count;
        }

        if($number<0)
            $number=0;


        //用户信息数组
        $activity_array = array(
            'startTime'	 =>  $config_array['startTime'],//活动开始时间
            'endTime'	     => $config_array['endTime'],//活动结束时间
            'activity_num' => $number,//蛋糕所剩数量
            'send_start_time' => $config_array["send_start_time"], //配送开始日期
            'send_end_time' => $config_array["send_end_time"], //配送结束日期
            'cityID'        => $config_array["cityID"], //活动城市ID
            'secret_key' =>$secret_key, //秘钥
            'love_letter' => $love_letter, //情书内容
            'paytypeID' =>$config_array["paytypeID"] //活动在线支付paytypeID
        );

        return $activity_array;

    }


    /**
     * 记录情书内容
     * @author  2015-01-27 allen.qiang <allen.qiang@fistforward.com>
     */
    public  function activity_love_letter($arg_array) {
        global $G_SHOP;

        //sql字符串
        $sql = " ";

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //秘钥
        if (isset($arg_array["secret_key"]) && !empty($arg_array["secret_key"])) {
            $sql .= ' AND extend_4 = ?';
            $params_type .= 's';
            $params_array[] = $arg_array["secret_key"];
        }

        //会员ID
        $pcustID = $G_SHOP->currentPCustID;

        try{
            $G_SHOP->DBCA->beginTransaction();

            $activities_extend = new entity('activities_extend');
            $activities_extend->activityNumber = $this->configName;//活动编号
            $activities_extend->classifyID = 0;
            $activities_extend->extend_1 = $pcustID;
            if($G_SHOP->DBCA->fetch($activities_extend) ) {
                $sql="SELECT COUNT(1) AS item_count FROM `activities_extend` WHERE activityNumber='".$this->configName."'".$sql." AND extend_4 <> '".$activities_extend->extend_4."'";
                $item_master = &$G_SHOP->DBCA->stmt_query($sql,$params_type,$params_array);
                if($item_master && $item_master[0]->item_count>0){
                    $G_SHOP->DBCA->commit();
                    return array( 'code' => 700, 'message' => '秘钥已存在，请重新设置' );
                }else{
                    $activities_extend->creator = $pcustID; //创建人
                    $activities_extend->extend_2 = date('Y-m-d H:i:s'); //提交情书时间
                    $activities_extend->extend_4 = $arg_array["secret_key"]; //秘钥
                    $activities_extend->extend_9 = $arg_array["love_letter"]; //情书内容
                    if (!$G_SHOP->DBCA->save($activities_extend)) {
                        throw new Exception('页面已过期，请刷新页面重试', 404);
                    }
                }

                $G_SHOP->DBCA->commit();
                return array( 'code' => 200, 'message' => '情书修改成功！' );
            }else{
                $sql="SELECT COUNT(1) AS item_count FROM `activities_extend` WHERE activityNumber='".$this->configName."'".$sql;
                $item_master = &$G_SHOP->DBCA->stmt_query($sql,$params_type,$params_array);
                if($item_master && $item_master[0]->item_count>0){
                    $G_SHOP->DBCA->commit();
                    return array( 'code' => 700, 'message' => '秘钥已存在，请重新设置' );
                }else{
                    //保存情书内容
                    $activities_extend = new entity('activities_extend');
                    $activities_extend->creator = $pcustID; //创建人
                    $activities_extend->activityNumber = $this->configName; //活动编号
                    $activities_extend->classifyID = 0; //0：未下单，已填写情书；1：已提交订单
                    $activities_extend->extend_1 = $pcustID; //会员ID
                    $activities_extend->extend_2 = date('Y-m-d H:i:s'); //提交情书时间
                    $activities_extend->extend_4 = $arg_array["secret_key"]; //秘钥
                    $activities_extend->extend_9 = $arg_array["love_letter"]; //情书内容
                    if (!$G_SHOP->DBCA->save($activities_extend)) {
                        throw new Exception('页面已过期，请刷新页面重试', 404);
                    }
                }

                $G_SHOP->DBCA->commit();
                return array( 'code' => 200, 'message' => '情书保存成功！' );
            }

        }
        catch (Exception $e) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 700, 'message' => '系统错误' );
            }
        }

    }

    /**
     * 根据秘钥显示情书内容
     * @author  2015-01-27 allen.qiang <allen.qiang@fistforward.com>
     */
    public  function activity_love_letter_show($arg_array) {
        global $G_SHOP;

        //sql字符串
        $sql = " ";

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //秘钥
        if (isset($arg_array["secret_key"]) && !empty($arg_array["secret_key"])) {
            $sql .= ' AND extend_4 = ?';
            $params_type .= 's';
            $params_array[] = $arg_array["secret_key"];
        }

        $sql_master="SELECT * FROM `activities_extend` WHERE activityNumber='".$this->configName."' AND classifyID='1' ".$sql;
        $temp_item_master = &$G_SHOP->DBCA->stmt_query($sql_master,$params_type,$params_array);
        if($temp_item_master){
            $activities_extend=array(
                'secret_key' => $temp_item_master[0]->extend_4,
                'love_letter' => $temp_item_master[0]->extend_9
            );
            $sql_master2="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND bill_code='".$temp_item_master[0]->extend_5."'";
            $item_master = &$G_SHOP->DBCA->stmt_query($sql_master2);
            if($item_master && $item_master[0]->item_count>0){
                return array( 'code' => 200, 'message' => '成功','data'=>$activities_extend );
            }else{
                return array( 'code' => 400, 'message' => '"爱情秘钥"已失效！' );
            }

        }else{
            return array( 'code' => 400, 'message' => '"爱情秘钥"不正确或不存在！' );
        }

    }

    /**
     * 下单成功修改活动状态
     * @author  2015-01-28 allen.qiang <allen.qiang@fistforward.com>
     */
    public  function update_status($arg_array) {
        global $G_SHOP;
        //会员ID
        $pcustID = $G_SHOP->currentPCustID;

        //修改活动状态
        $activities_extend = new entity('activities_extend');
        $activities_extend->activityNumber = $this->configName; //活动编号
        $activities_extend->classifyID = 0; //0：未下单，已填写情书；1：已提交订单
        $activities_extend->extend_1 = $pcustID; //会员ID
        if($G_SHOP->DBCA->fetch($activities_extend) ) {
            $activities_extend->classifyID = 1;
            $activities_extend->extend_3 = date('Y-m-d H:i:s'); //下单时间
            $activities_extend->extend_5 = $arg_array["bill_code"];//订单编号
            $activities_extend->extend_6 = $arg_array["bill_no"];//订单号
            if (!$G_SHOP->DBCA->save($activities_extend)) {
                throw new Exception('页面已过期，请刷新页面重试', 404);
            }
            return array( 'code' => 200, 'message' => '成功');
        }else{
            return array( 'code' => 400, 'message' => '失败');
        }

    }

} 